package com.ease.common.exception;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;

/**
 * 
 * @author nathanleewei
 * 
 */
public class ExceptionBean implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -7470018404382002554L;

	private Throwable throwable;

	public ExceptionBean(Throwable throwable) {
		this.setThrowable(throwable);

	}

	public String getMessage() {
		if (null != throwable) {
			return throwable.getMessage() + "\n" + throwable.getClass() + "\n";
		}
		return null;
	}

	public String getStackTrace() {
		if (null != throwable) {
			StringWriter writer = new StringWriter();
			PrintWriter printWriter = new PrintWriter(writer);
			throwable.printStackTrace(printWriter);
			String stackTrace = writer.toString();
			printWriter.close();

			try {
				if (null != writer) {
					writer.close();
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} finally {
				printWriter = null;
				writer = null;
			}
			return stackTrace;
		}
		return null;
		// ByteArrayOutputStream out = new ByteArrayOutputStream();
		// try {
		// e.printStackTrace(new PrintStream(out));
		// } catch (Exception ex) {
		// ex.printStackTrace();
		// }finally{
		// try {
		// out.close();
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
		// }
		//
		// return new String(out.toByteArray());
	}

	/**
	 * @return Returns the throwable.
	 */
	public Throwable getThrowable() {
		return throwable;
	}

	/**
	 * @param throwable
	 *            The throwable to set.
	 */
	public void setThrowable(Throwable throwable) {
		this.throwable = throwable;
	}

}
